cc.Class({

    extends: cc.Component,
    properties: {
    },
    onLoad() {
        window.CardViewData = {};
        this.initView();
        this.initData();
        this.initTestData();
    },
    initView() {
        this.initComponent();
    },
    initData() {
        window.CardViewData.dynamicData = {};
        window.CardViewData.cardArr = new Array();
        window.CardViewData.dynamicData.cardArrChange = true;
        // window.CardViewData.dynamicData.brave = new window.Brave();

        window.CardViewData.rowNum = 3;
        window.CardViewData.colNum = 3;

        window.CardViewData.dynamicData.brave = {};
        window.CardViewData.dynamicData.brave.hp = 10;
        window.CardViewData.dynamicData.brave.hasWeapon = false;
        window.CardViewData.dynamicData.brave.weapon = {};
        // window.CardViewData.dynamicData.brave

        for (var i = 0; i < 9; i++) {
            var tmpNode = null;
            if (i == 2) {
                tmpNode = cc.instantiate(window.CardViewData.braveCard);
                tmpNode.getComponent("CardComponent").type = 1;
                window.CardViewData.dynamicData.brave.cmpt = tmpNode.getComponent("CardComponent");
            } else {
                // tmpNode = cc.instantiate(window.CardViewData.genCard);
                tmpNode = cc.instantiate(window.CardViewData.baseCard);
                tmpNode.getComponent("CardComponent").bgValue = 0;
                tmpNode.getComponent("CardComponent").mainCenterValue = i % 3 == 0 ? "baba" : i % 3 == 1 ? "biandang" : "mama";
                tmpNode.getComponent("CardComponent").topRightValue = 1;
                tmpNode.getComponent("CardComponent").topRightLbValue = 10;
            }
            tmpNode.getComponent("CardComponent").index = i;
            window.CardViewData.cardArr.push(tmpNode.getComponent("CardComponent"));
            window.CardViewData.selfLayout.node.addChild(tmpNode);
        }

        window.CardViewData.dynamicData.cardArrChange = false;
        // 修改布局大小
        window.CardViewData.selfLayout.node.width = window.CardViewData.genCard.width * window.CardViewData.rowNum
            + window.CardViewData.selfLayout.paddingLeft + window.CardViewData.selfLayout.paddingRight
            + window.CardViewData.selfLayout.spacingX * (window.CardViewData.rowNum - 1);

        window.CardViewData.selfLayout.node.height = window.CardViewData.genCard.height * window.CardViewData.colNum
            + window.CardViewData.selfLayout.paddingTop + window.CardViewData.selfLayout.paddingBottom
            + window.CardViewData.selfLayout.spacingY * (window.CardViewData.colNum - 1);

        window.CardViewData.selfLayout.updateLayout();
    },
    initTestData() {
    },
    initComponent: function () {

        window.CardViewData.cardView = this;
        window.CardViewData.button1 = cc.find("button1", this.node).getComponent(cc.Button);
        window.CardViewData.button2 = cc.find("button2", this.node).getComponent(cc.Button);
        window.CardViewData.button3 = cc.find("button3", this.node).getComponent(cc.Button);

        window.CardViewData.selfLayout = cc.find("selfLayout", this.node).getComponent(cc.Layout);
        // window.CardViewData.landNode = cc.find("landNode", this.node);
        // window.CardViewData.manLandNode = cc.find("manLandNode", this.node);

        window.CardViewData.genCard = cc.find("genCard", this.node);
        window.CardViewData.braveCard = cc.find("braveCard", this.node);
        window.CardViewData.baseCard = cc.find("baseCard", this.node);


    },

    update: function (dt) {
        this.refreshTokenView();
    },
    refreshTokenView() {
        if (window.CardViewData.dynamicData.cardArrChange) {
            window.CardViewData.selfLayout.node.removeAllChildren(false);

            for (var i = 0; i < window.CardViewData.cardArr.length; i++) {
                // 通过判断子节点是否处于layout中，如果没在的话，则添加
                // if (!window.GameViewData.dynamicData.func2TokenArr[i].node.isChildOf(window.GameViewData.tokenLayout.node)) {
                // window.GameViewData.func2Layout.node.addChild(window.GameViewData.dynamicData.func2TokenArr[i].node);
                // }
                if (!window.CardViewData.cardArr[i].node.isChildOf(window.CardViewData.selfLayout.node)) {
                    window.CardViewData.selfLayout.node.addChild(window.CardViewData.cardArr[i].node);
                }

            }
            console.log(window.CardViewData.cardArr);
            window.CardViewData.dynamicData.cardArrChange = false;
        }

    },

    onEnable: function () {
        this.addListener();
    },
    onDisable: function () {
        this.removeListener();
    },
    addListener: function () {
        this.button1Event = window.CardViewData.button1.node.on(cc.Node.EventType.TOUCH_END, this.button1Click.bind(this));
        this.button2Event = window.CardViewData.button2.node.on(cc.Node.EventType.TOUCH_END, this.button2Click.bind(this));
        this.button3Event = window.CardViewData.button3.node.on(cc.Node.EventType.TOUCH_END, this.button3Click.bind(this));
    },
    removeListener: function () {
        this.button1.node.off(cc.Node.EventType.TOUCH_END, window.CardViewData.button3Event);
        this.button2.node.off(cc.Node.EventType.TOUCH_END, window.CardViewData.button2Event);
        this.button1.node.off(cc.Node.EventType.TOUCH_END, window.CardViewData.button3Event);
    },
    button1Click: function () {
        console.log("button1Click");
    },
    button2Click: function () {
        console.log("button2Click");
    },
    button3Click: function () {
        console.log("button3Click");
    },

});
